//
//  KTRootTabBarC.m
//  KTLotteryTicket
//
//  Created by CoolKi on 16/3/4.
//  Copyright © 2016年 CoolKi. All rights reserved.
//

#import "KTRootTabBarC.h"

#import "KTTabBarView.h"

#import "KTDiscoverTVC.h"
#import "KTHistoryVC.h"
#import "KTMyLotteryVC.h"
#import "KTArenaVC.h"
#import "KTHallVC.h"

#import "KTBaseNavVC.h"
#import "KTArenaNavVC.h"


@interface KTRootTabBarC ()<KTTabBarViewDelegate>

@property (nonatomic,strong)NSMutableArray * tabBarItemArr;

@end

@implementation KTRootTabBarC

-(NSMutableArray *)tabBarItemArr{
    if (_tabBarItemArr == nil) {
        _tabBarItemArr = [NSMutableArray array];
        return _tabBarItemArr;
    }
    return _tabBarItemArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 添加5个子视图控制器
    [self addAllVC];
    
    // 移除系统自带的tabBar
    //[self.tabBar removeFromSuperview];
    

    
    //自定义tabBar
    KTTabBarView * tabBarView = [[KTTabBarView alloc]init];
    tabBarView.frame = self.tabBar.bounds;
    tabBarView.backgroundColor = [UIColor greenColor];
    tabBarView.delegate = self;
    tabBarView.tabBarItemArr = self.tabBarItemArr;
    
    [self.tabBar addSubview:tabBarView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    for (UIView * view in self.tabBar.subviews) {
        //NSLog(@"view~%@",view);
        if (![view isKindOfClass:[KTTabBarView class]]) {
            [view removeFromSuperview];
        }
    }
    
}

#pragma mark - KTTabBarViewDelegate
- (void)tabBarView:(KTTabBarView *)tabbarView Index:(NSUInteger)index{
    self.selectedIndex = index;
}

- (void)addAllVC{
    //购彩大厅
    KTHallVC * hallVC = [[KTHallVC alloc]init];
    hallVC.view.backgroundColor = [UIColor orangeColor];
    [self addOneVCWithViewController:hallVC Title:@"购彩大厅" Img:[UIImage imageNamed:@"TabBar_LotteryHall_new"] SelectImg:[UIImage imageNamed:@"TabBar_LotteryHall_selected_new"]];
    

    //竞技场
    KTArenaVC *arenaVC = [[KTArenaVC alloc] init];
    //arenaVC.view.backgroundColor = [UIColor yellowColor];
    [self addOneVCWithViewController:arenaVC Title:@"竞技场" Img:[UIImage imageNamed:@"TabBar_Arena_new"] SelectImg:[UIImage imageNamed:@"TabBar_Arena_selected_new"]];
    
    //发现
    UIStoryboard * storyB = [UIStoryboard storyboardWithName:@"KTDiscoverTVC" bundle:nil];
    UIViewController * dicTVC = [storyB instantiateInitialViewController];
    
//    KTDiscoverTVC *disVC = [[KTDiscoverTVC alloc] init];
//    disVC.view.backgroundColor = [UIColor greenColor];
    [self addOneVCWithViewController:dicTVC Title:@"发现" Img:[UIImage imageNamed:@"TabBar_Discovery_new"] SelectImg:[UIImage imageNamed:@"TabBar_Discovery_selected_new"]];
    
    //开奖信息
    KTHistoryVC *hisVC = [[KTHistoryVC alloc] init];
    hisVC.view.backgroundColor = [UIColor grayColor];
    [self addOneVCWithViewController:hisVC Title:@"发现" Img:[UIImage imageNamed:@"TabBar_History_new"] SelectImg:[UIImage imageNamed:@"TabBar_History_selected_new"]];
    
    
    //我的彩票
    KTMyLotteryVC *myLottery = [[KTMyLotteryVC alloc] init];
    [self addOneVCWithViewController:myLottery Title:@"我的彩票" Img:[UIImage imageNamed:@"TabBar_MyLottery_new"] SelectImg:[UIImage imageNamed:@"TabBar_MyLottery_selected_new"]];
}


- (void)addOneVCWithViewController:(UIViewController *)viewController Title:(NSString *)title Img:(UIImage *)image SelectImg:(UIImage *)selectImg{
    
    viewController.title = title;
    viewController.tabBarItem.image = image;
    viewController.tabBarItem.selectedImage = selectImg;
    
    //
    [self.tabBarItemArr addObject:viewController.tabBarItem];
    
    if ([viewController isKindOfClass:[KTArenaVC class]]) {
        KTArenaNavVC * arenaNavVC = [[KTArenaNavVC alloc]initWithRootViewController:viewController];
        [self addChildViewController:arenaNavVC];
    }else{
        KTBaseNavVC * baseNavVC = [[KTBaseNavVC alloc]initWithRootViewController:viewController];
        [self addChildViewController:baseNavVC];
    }
 
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
















@end
